package com.ofood.food.controller;

import com.ofood.domain.Food;
import com.ofood.result.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/food/provider")
public class FoodController {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${food.service.url}")
    private String foodServiceUrl;

    @RequestMapping("/get/{id}")
    public BaseResult<Food> getFoodById(@PathVariable("id") Long foodId){
        String url=foodServiceUrl+"/food/get/"+foodId;
        return restTemplate.getForObject(url,BaseResult.class);
    }

    @RequestMapping("/get")
    public BaseResult<Food> getAllFood(){
        String url=foodServiceUrl+"/food/get/";
        return restTemplate.getForObject(url,BaseResult.class);
    }

    @RequestMapping("/post")
    public BaseResult<Boolean> insertFood(@RequestBody Food food){
        String url=foodServiceUrl+"/food/post/";
        return restTemplate.postForObject(url,food,BaseResult.class);
    }

    @RequestMapping("/put")
    public BaseResult<Boolean> updateFood(@RequestBody Food food){
        String url=foodServiceUrl+"/food/put/";
        return restTemplate.postForObject(url,food,BaseResult.class);
    }

    @RequestMapping("/delete/{id}")
    public BaseResult<Boolean> deleteFood(@PathVariable("id") Long foodId){
        String url=foodServiceUrl+"/food/delete/"+foodId;
        return restTemplate.getForObject(url,BaseResult.class);
    }
}
